/**
 *	OpenSource code or fragments by agile webentwicklung, Marcus Fuhrmeister
 *
 *	Redistribution and use in source and binary forms, with or without
 *	modification, are permitted provided that the following conditions
 *	are met:
 *  
 *		* Redistribution of source code must retain the above OpenSource comment,
 *		this list of conditions and the following disclaimer.
 * 
 *		* Redistribution in binary form must reproduce the above OpenSource comment,
 *		this list of conditions and the following disclaimer in the
 *		documentation and/or other materials provided with the distribution.
 * 
 *	Neither the name of 'agile webentwicklung, Marcus Fuhrmeister' or the
 *	names of contributors may be used to endorse or promote products derived
 *	from this software without specific prior written permission.
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *	Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 
 */
package de.fuhrmeister.util;

import javax.swing.JOptionPane;

public class MessageBox {

	private static MessageBox messageBox;
	public static final int ERROR_MESSAGE = JOptionPane.ERROR_MESSAGE;
	public static final int INFORMATION_MESSAGE = JOptionPane.INFORMATION_MESSAGE;
	public static final int WARNING_MESSAGE = JOptionPane.WARNING_MESSAGE;
	public static final int QUESTION_MESSAGE = JOptionPane.QUESTION_MESSAGE;
	public static final int PLAIN_MESSAGE = JOptionPane.PLAIN_MESSAGE;
	public final int ANSWER_YES = JOptionPane.YES_OPTION;
	public final int ANSWER_NO = JOptionPane.NO_OPTION;
	public final int ANSWER_CANCEL = JOptionPane.CANCEL_OPTION;

	/**
	 * Singleton pattern.
	 * 
	 * @return this
	 */
	public static MessageBox getInstance() {
		if (messageBox == null) {
			messageBox = new MessageBox();
			return MessageBox.messageBox;
		}
		return MessageBox.messageBox;
	}

	/**
	 * Shows an information dialog.
	 * 
	 * @param component the component the dialog is bound to
	 * @param title the title of the dialog
	 * @param message the message to be displayed
	 */
	public void inform(final java.awt.Component component, final String title, final String message) {
		JOptionPane.showMessageDialog(component, message, title, JOptionPane.INFORMATION_MESSAGE);
	}

	/**
	 * Shows an alert dialog.
	 * 
	 * @param component the component the dialog is bound to
	 * @param title the title of the dialog
	 * @param message the message to be displayed
	 * @param type the type of the dialog
	 */
	public void alert(final java.awt.Component component, final String title, final String message, final int type) {
		JOptionPane.showMessageDialog(component, message, title, type);
	}

	/**
	 * Shows an error dialog.
	 * 
	 * @param component the component the dialog is bound to
	 * @param title the title of the dialog
	 * @param message the message to be displayed
	 */
	public void error(final java.awt.Component component, final String title, final String message) {
		this.alert(component, title, message, JOptionPane.ERROR_MESSAGE);

	}

	/**
	 * Shows an option dialog.
	 * 
	 * @param component the component the dialog is bound to
	 * @param title the title of the dialog
	 * @param message the message to be displayed
	 * @param options the options to be displayed
	 */
	public int question(final java.awt.Component component, final String title, final String message, final String[] options) {
		if (options.length <= 2) {
			return JOptionPane.showOptionDialog(component, message, title, JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
		} else {
			return JOptionPane.showOptionDialog(component, message, title, JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null, options, options[0]);
		}
	}
}
